//
//  HJHVisitorLoginView.swift
//  Weibo
//
//  Created by 黄健桦 on 2016/9/24.
//  Copyright © 2016年 黄健桦. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol HJHVisitorLoginViewDelegate: NSObjectProtocol {
    
    func userWillLogin()
    
    @objc optional func userWillReigster()
}

class HJHVisitorLoginView: UIView {
    
    weak var delegate: HJHVisitorLoginViewDelegate?
    
    func updateInfo(tipText: String,imageName: String? = nil){
        
        tipLabel.text = tipText
        
        if imageName == nil {
            
            startAnimation()
            
        }
        else{
            circleView.image = UIImage(named: imageName!)
            
            iconView.isHidden = true
            
            bringSubview(toFront: circleView)
        }
        
    }
    
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        
        animation.toValue = M_PI * 2
        
        animation.duration = 30
        
        animation.isRemovedOnCompletion = false
        
        circleView.layer.add(animation, forKey: nil)
        
    }
    
    //添加子视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setUpUI() {
        addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //注释
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(16)
            make.centerX.equalTo(circleView)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        let value = CGFloat(237 / 255.0)
        
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
        
    }
    
    @objc private func loginBtnDidClick() {
        
        delegate?.userWillLogin()
        
    }
    
    @objc private func registerBtnDidClick() {
        
        delegate?.userWillReigster?()
        
    }
    
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈", textColor: UIColor.darkGray, fontSize: 14)
        
        //自动换行
        l.numberOfLines = 0
        //最大布局宽度
        l.preferredMaxLayoutWidth = 224
        //居中对齐
        l.textAlignment = .center
        
        return l
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
}
